package com.neusoft.blog.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.blog.bean.Article;
import com.neusoft.blog.bean.ArticlePo;
import com.neusoft.blog.bean.Echars;
import com.neusoft.blog.bean.RespBean;
import com.neusoft.blog.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ArticleService {
    @Autowired
    ArticleMapper articleMapper;


    public RespBean saveAticle(Article article) {
        int count = articleMapper.saveAticle(article);
        if (article.getStatus() == 1) {
            if (count > 0) {
                return RespBean.ok("发表文章成功", article);
            }
            return RespBean.error("发表文章失败");
        } else if (article.getStatus() == 2) {
            if (count > 0) {
                return RespBean.ok("保存文章成功", article);
            }
            return RespBean.error("保存文章失败");
        } else {
            return RespBean.error("未知文章错误");
        }
    }

    public RespBean updeteAticle(Article article) {
        int count = articleMapper.updeteAticle(article);
        if (count > 0) {
            return RespBean.ok("修改文章成功", article);
        }
        return RespBean.error("修改文章失败");
    }

    public PageInfo getArticls(ArticlePo articlePo) {
        PageHelper.startPage(articlePo.getPageNum(), articlePo.getPageSize());
        List<Article> list = articleMapper.getArticls(articlePo);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    public Article getArticlsById(Integer aid) {
        return articleMapper.getArticlsById(aid);
    }

    public int updeteCount(Integer count,Integer aid) {
        return articleMapper.updeteCount(count,aid);
    }

    public RespBean delArticl(Integer id) {
            int count =articleMapper.delArticl(id);
            if(count>0){
                return RespBean.ok("删除成功",null);
            }
            return  RespBean.error("删除失败");

    }

    public RespBean multiDel(Integer[] ids) {

            int count =articleMapper.multiDel(ids);
            if(count>0){
                return RespBean.ok("批量删除成功",null);
            }
            return  RespBean.error("批量删除失败");
        }

    public List<Echars> echarsByName() {
        return  articleMapper.echarsByName();
    }
}